package com.ljh.onlinetest.controller.student;

import com.ljh.onlinetest.base.BaseApiController;
import com.ljh.onlinetest.base.RestResponse;
import com.ljh.onlinetest.domain.User;
import com.ljh.onlinetest.domain.UserEventLog;
import com.ljh.onlinetest.domain.enums.RoleEnum;
import com.ljh.onlinetest.domain.enums.UserStatusEnum;
import com.ljh.onlinetest.event.UserEvent;
import com.ljh.onlinetest.service.AuthenticationService;
import com.ljh.onlinetest.service.MessageService;
import com.ljh.onlinetest.service.UserEventLogService;
import com.ljh.onlinetest.service.UserService;
import com.ljh.onlinetest.utils.DateTimeUtil;
import com.ljh.onlinetest.viewmodel.student.dashboard.UserRegisterVM;
import com.ljh.onlinetest.viewmodel.student.user.UserEventLogVM;
import com.ljh.onlinetest.viewmodel.student.user.UserResponseVM;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

/**
 * @author 余一
 * @date 2022/2/27  21:05
 */
@RestController("StudentUserController")
@RequestMapping("/api/student/user")
public class UserController extends BaseApiController {

    private final UserService userService;
    private final UserEventLogService userEventLogService;
    private final MessageService messageService;
    private final AuthenticationService authenticationService;
    private final ApplicationEventPublisher appEventPublisher;

    @Autowired
    public UserController(UserService userService, UserEventLogService userEventLogService, MessageService messageService, AuthenticationService authenticationService, ApplicationEventPublisher appEventPublisher) {
        this.userService = userService;
        this.userEventLogService = userEventLogService;
        this.messageService = messageService;
        this.authenticationService = authenticationService;
        this.appEventPublisher = appEventPublisher;
    }

    @PostMapping("register")
    public RestResponse register(@RequestBody @Valid UserRegisterVM model){
        User user = userService.getUserByUserName(model.getUserName());
        if (null != user) {
            return new RestResponse(2,"用户已存在");
        }
        User newUser = modelMapper.map(model, User.class);
        //加密明文密码
        String encodePwd = authenticationService.pwdEncode(model.getPassword());
        Date now = new Date();
        newUser.setPassword(encodePwd);
        newUser.setUserUuid(UUID.randomUUID().toString());
        newUser.setRole(RoleEnum.STUDENT.getCode());
        newUser.setStatus(UserStatusEnum.Enable.getCode());
        newUser.setLastActiveTime(now);
        newUser.setDeleted(false);
        newUser.setCreateTime(now);
        userService.insertByFilter(newUser);
        UserEventLog userEventLog = new UserEventLog(newUser.getId(), newUser.getUserName(), newUser.getRealName(), new Date());
        userEventLog.setContent("欢迎" + newUser.getUserName() + "注册在线考试系统");
        appEventPublisher.publishEvent(new UserEvent(userEventLog));
        return RestResponse.ok();
    }

    @PostMapping("/current")
    public RestResponse<UserResponseVM> current(){
        User user = getCurrentUser();
        UserResponseVM vm = UserResponseVM.from(user);
        return RestResponse.ok(vm);
    }

    /**
     * 用户动态log
     * @return
     */
    @PostMapping("/log")
    public RestResponse<List<UserEventLogVM>> log(){
        User user = getCurrentUser();
        List<UserEventLog> userEventLogs = userEventLogService.getUserEventLogByUserId(user.getId());
        List<UserEventLogVM> userEventLogVMS = userEventLogs.stream().map(d -> {
            UserEventLogVM vm = modelMapper.map(d, UserEventLogVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(d.getCreateTime()));
            return vm;
        }).collect(Collectors.toList());
        return RestResponse.ok(userEventLogVMS);
    }
}
